
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  database;

import java.util.Enumeration;
import java.util.Hashtable;

import logger.PoolLogger;



import dbmanager.DBManager;

public class RecordInsert
{
	private String table = "property_details";
	private Hashtable<Object, Object> newHash;
	private String query = null;
	private String insertFields = "";
	private String insertValues = "";

	PoolLogger pl;
	
	public RecordInsert() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	/*public void setDatabase(DBManager database)
    {	
    	this.database = database;
    }*/
    
	
	public void setTableName(String tableName) 
    {
		this.table = tableName;	
    }
    

    /**
     * setInsertHash() method will sets the condition for the table query part.
     * */
    public void setHash(Hashtable<Object, Object> condition)
	{
    	this.newHash = new Hashtable<Object, Object>();
		this.newHash.putAll(condition);
		insertHashQuery(); // Calls the method for converting into the String.
	}

    /**
     * insertHashQuery() - It will creats the string from the Hash Table. Which get stores into the respected string variable.  
     * */    
    private void insertHashQuery() 
    {
    	try 
		{
			if(newHash.isEmpty())
			{
				System.out.println(" Field(s) or Value(s) doesn't exists into the supplied query ");
				pl.info(" Field(s) or Value(s) doesn't exists into the supplied query ");
    			pl.debug(" Field(s) or Value(s) doesn't exists into the supplied query ");
    			pl.warn(" Field(s) or Value(s) doesn't exists into the supplied query ");
    			pl.fatal(" Field(s) or Value(s) doesn't exists into the supplied query ");
    			pl.error(" Field(s) or Value(s) doesn't exists into the supplied query ");
    			pl.trace(" Field(s) or Value(s) doesn't exists into the supplied query ");
			}
			else
			{
				Enumeration keys = newHash.keys();
				while(keys.hasMoreElements())
				{
					String key = keys.nextElement().toString();
					String value = newHash.get(key).toString();
					insertFields = insertFields + key + ", ";
					insertValues = insertValues + " '"+ value + "', ";
				}
				insertFields = insertFields.substring(0, insertFields .length() - 2);
				insertValues = insertValues.substring(0, insertValues .length() - 2);
			}
		} 
		
		catch (Exception e) { }
	}
    
    public synchronized int getInsert()
    {
      try
  	  { 		  
  		  query = "INSERT INTO " + table;
  		  
  		  try 
  		  {
			if(insertFields.length() != 0)
				  query = query + "(" + insertFields + ")";
		  } 
  		  catch (Exception e)  { }
  		  
  		  try 
  		  {
			if(insertValues.length() != 0)
				  query = query + " VALUES (" + insertValues + ")";
		  } 
  		  catch (Exception e) { }
  		  
  		  System.out.println("The query is : " + query);
  		  pl.info("The query is : " + query);
  		  pl.debug("The query is : " + query);
  		  pl.warn("The query is : " + query);
  		  pl.fatal("The query is : " + query);
  		  pl.error("The query is : " + query);
  		  pl.trace("The query is : " + query);
  		  
  		  int j = DBManager.getInsert(query.toString());
  		  //System.out.println(j);
  		  if(j>0)
  		  {
  			 System.out.println("Data inserted successfully");
  			 pl.info("Data inserted successfully");
    		 pl.debug("Data inserted successfully");
    		 pl.warn("Data inserted successfully");
    		 pl.fatal("Data inserted successfully");
    		 pl.error("Data inserted successfully");
    		 pl.trace("Data inserted successfully");
  		  }
  		  return j;
  	  }
  	  catch(Exception e)
  	  {
  		  e.printStackTrace();
  	  }
  	  return 0;
    }
    
    public static void main(String[] args) {
    	Hashtable<Object, Object> old = new Hashtable<Object, Object>();
    	old.put("mid", "9377");
		old.put("pid", "9");
		//newHash.put("vt", "knowledge");
		old.put("pv", "9322");
		
		RecordInsert ri = new RecordInsert();
		ri.setHash(old);
		ri.getInsert();
	}
}
